package guiLayer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import modelLayer.Forest;
import modelLayer.Owner;
import controlerLayer.ForestCtr;
import controlerLayer.OwnerCtr;


public class CreaterForest extends Creater {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Forest f;
	private Updater updater;
	@SuppressWarnings("unused")
	private String ownerName;
	/**
	 * Create the panel.
	 */
	public CreaterForest(final Forest f, final Updater updater) {
		lblTitle.setText("Create Forest");
		cmb.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				if(arg0.getStateChange() == ItemEvent.SELECTED){
					ownerName = ((String)arg0.getItem());
				}
			}
		});
		lblCmb.setText("Owner");
		this.f = f;
		this.updater = updater;
		
		btnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				createForest();
			}
		});
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(f != null){
					updater.closeMe(CreaterForest.this);
				}else{
					clear();
				}
			}
		});
		txtItem3.setVisible(false);
		lblItem3.setVisible(false);
		lblItem2.setText("Location");
		lblItem1.setText("Name");
		
		if(f == null){
			btnCreate.setText("Create");
			btnClear.setText("Clear");
		}else if(f != null){
			cmb.setSelectedItem(f.getOwner());
			btnClear.setText("Cancel");
			btnCreate.setText("Update");
			txtItem1.setText(f.getName());
			txtItem2.setText(f.getLocation());
		}
		
		addAncestorListener(new AncestorListener() {
			public void ancestorAdded(AncestorEvent arg0) {
				redrawOwner();
			}
			public void ancestorMoved(AncestorEvent arg0) {
			}
			public void ancestorRemoved(AncestorEvent arg0) {
			}
		});
	}

	@Override
	protected void clear() {
		super.clear();
	}

	protected void createForest() {
		if(checkInput()){
			String name = txtItem1.getText();
			String location = txtItem2.getText();
			if(f == null){
				ForestCtr fCtr = new ForestCtr();
				fCtr.addForest(name, location);
				if(!cmb.getSelectedItem().equals("None")){
					OwnerCtr oCtr = new OwnerCtr();
					oCtr.addForestToOwner((String)cmb.getSelectedItem(), name);
				}
				clear();
				created(lblCreated, "Forest " + name + " created!");
			}else if(f != null){
				f.setName(name);
				f.setLocation(location);
				if(f.getOwner() == null && !cmb.getSelectedItem().equals("None")){
					OwnerCtr oCtr = new OwnerCtr();
					oCtr.addForestToOwner((String)cmb.getSelectedItem(), name);
				}else if(!cmb.getSelectedItem().equals("None") && !f.getOwner().getName().equals(cmb.getSelectedItem())){
					f.getOwner().removeForest(f);
					OwnerCtr oCtr = new OwnerCtr();
					oCtr.addForestToOwner((String)cmb.getSelectedItem(), name);
				}else if(cmb.getSelectedItem().equals("None")){
					if(f.getOwner() != null){
						f.getOwner().removeForest(f);
						f.setOwner(null);
					}
				}
				
				updater.closeMe(this);
			}
		}
	}
	
	protected boolean checkInput(){
		boolean ret = true;
		
		if(txtItem1.getText().length() <= 0){
			error(lblErrorItem1, "Name cant be empty!");
			ret = false;
		}else{
			lblErrorItem1.setText("");
		}
		
		if(txtItem2.getText().length() <= 0){
			error(lblErrorItem2, "Location cant be empty!");
			ret = false;
		}else{
			lblErrorItem2.setText("");
		}
		
		return ret;
	}
	
	
	private void redrawOwner(){
		String selected = null;
		
		if(cmb.getItemCount() > 0){
			selected = (String)cmb.getSelectedItem();
		}
		
		DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
		OwnerCtr oCtr = new OwnerCtr();
		ArrayList<Owner> owners = oCtr.getAllOwners();
		model.addElement("None");
		for(Owner o : owners){
			model.addElement(o.getName());
		}
		
		cmb.setModel(model);
		
		if(f != null && selected == null){
			if(f.getOwner() != null){
				cmb.setSelectedItem(f.getOwner().getName());
			}else{
				cmb.setSelectedItem("None");
			}
		}else if(selected != null){
			cmb.setSelectedItem(selected);
		}
		
	}
	
}
